<template>
  <header class="todo-header">
    <div class="add-todo">
      <input
        type="text"
        placeholder="请输入城市名称"
        v-model.trim="content"
        @keyup.enter="addTodo"
      />
      <button @click="addTodo">搜索</button>
    </div>
    <ul v-if="content">
      <li>{{ content }}</li>
    </ul>
  </header>
</template>

<script setup>
import { ref } from 'vue';
const emit = defineEmits(['add-todo']);

const content = ref('');

const addTodo = () => {
  if (!content.value) {
    alert('请输入城市名称');
    return;
  }
  // 1. 构造待办对象
  const todo = {
    id: Date.now(),
    content: content.value,
    done: false,
  };
  // 2. 将对象发送到父组件(App)
  emit('add-todo', todo);

  // 3. 跳转到天气页面
  router.push({
    path: '/weather',
    query: {
      search: content.value,
    },
  });

  // 4. 清空输入框内容
  content.value = '';
};

import { useRouter } from 'vue-router';
const router = useRouter();
</script>

<style lang="scss" scoped>
.todo-header {
  background-color: #00668a;
  color: white;
  padding: 5px 0 10px;
  .add-todo {
    max-width: 1216px;
    display: flex;
    justify-content: flex-start;
    margin: 0 auto;
    margin-top: 10px;
    gap: 10px;
    input {
      flex: 1;
      height: 30px;
      padding: 0 10px;
      border-radius: 3px;
      outline: none;
      border: solid 1px white;
    }
    button {
      display: inline-block;
      width: 80px;
      height: 30px;
      background-color: #eee;
      color: #00668a;
      line-height: 30px;
      border: none;
      border-radius: 3px;
      &:hover {
        cursor: pointer;
        background-color: #fff;
        color: #ff6700;
      }
    }
  }
  ul li,p{
    max-width: 1216px;
    height: 2rem;
    line-height: 2rem;
    margin: 0 auto;
    padding-left: 10px;
    background: #004e71;
    list-style: none;
  }
}
</style>